package com.xinfu.newproject.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.ui.carFee.MonthCardFragment;
import com.xinfu.newproject.ui.carFee.TemporaryStopFragment;
import com.xinfu.newproject.ui.carFee.add_parking.AddMonthCardParkFragment;
import com.xinfu.newproject.ui.carFee.add_parking.AddTemporaryParkFragment;
import com.xinfu.newproject.interface_.SwichFragmentInterface;
import com.xinfu.newproject.adapter.TicketPagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ParkingActivity extends BaseActivity implements SwichFragmentInterface {

    @BindView(R.id.carFee_vp)
    ViewPager mViewPager;

    @BindView(R.id.carFee_magicIndicator)
    MagicIndicator mIndicator;

    private AddMonthCardParkFragment addMonthCardParkFragment;
    private AddTemporaryParkFragment addTemporaryParkFragment;

    private FragmentManager fragmentManager;
    public final static int FRAGMENT_ZERO = 0;
    public final static int FRAGMENT_ONE = 1;
    public final static int FRAGMENT_TWO = 2;


    private static final String[] CHANNELS = new String[]{"月卡车", "临停车"};
    private List<String> mDataList = Arrays.asList(CHANNELS);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("停车缴费");
        initViewBind();
        initIndicator();
        initLisener();

    }

    private void initLisener() {

    }

    private void initViewBind() {
        fragmentManager = getSupportFragmentManager();
        ArrayList<Fragment> mFraList = new ArrayList<>();
        MonthCardFragment monthCardFragment = MonthCardFragment.getInstance("monthCard");
        TemporaryStopFragment temporaryStopFragment = new TemporaryStopFragment();

        mFraList.add(monthCardFragment);
        mFraList.add(temporaryStopFragment);

        TicketPagerAdapter pagerAdapter = new TicketPagerAdapter(getSupportFragmentManager(), mFraList);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(pagerAdapter);
    }

    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        //自适应tab的个数居中,
        // 布局中让在xml里中将MagicIndicator套在Gravity为Center的布局中
        commonNavigator.setAdjustMode(true);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));

                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.gray_9));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.yellow_ff));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.yellow_ff));
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                return indicator;
            }
        });
        mIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerPadding(UIUtil.dip2px(this, 15));
        titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.simple_splitter));
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    public void showFragment(int index, int type) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragments(ft);
        //注意这里设置位置
//        position = index;
        switch (index) {
            case FRAGMENT_ZERO:
                break;
            case FRAGMENT_ONE:

                break;

        }
        ft.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

    }

    @Override
    public void setSelectedFragment(int fragment, int type) {
        showFragment(fragment, type);
    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }
}
